﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpSnapEndPointPriorityTypesPreprocessor.h"
#include "ISnap.h"
#include "ISnapContext.h"
#include "IElement.h"
#include "IGraphicsNodeReference.h"
#include "IDocument.h"
#include "ICombinedSnap.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

namespace
{
    class Comparer
    {
    public:
        Comparer(const std::vector<Int64>& types) : m_types(types)
        {
        }

        bool operator()(const OwnerPtr<gcmp::ISnap>& snap0, const OwnerPtr<gcmp::ISnap>& snap1)
        {
            DBG_WARN_AND_RETURN_UNLESS(snap0.get(), false, L"snap0为空",L"GDMPLab",L"2024-03-30");
            DBG_WARN_AND_RETURN_UNLESS(snap1.get(), true, L"snap1为空",L"GDMPLab",L"2024-03-30");

            if (snap0.get() == snap1.get())
                return false;

            bool isEndPointInTypes0 = IsEndPointInTypes(snap0.get());
            bool isEndPointInTypes1 = IsEndPointInTypes(snap1.get());
            if (isEndPointInTypes0 && !isEndPointInTypes1)
                return true;

            return false;
        }

    private:
        bool IsEndPointInTypes(const gcmp::ISnap* snap)
        {
            if (!snap)
                return false;

            IDocument* pDoc = snap->GetSnapContextFR()->GetDocument();
            DBG_WARN_AND_RETURN_UNLESS(pDoc, false, L"空指针pDoc",L"GDMPLab",L"2024-03-30");

            if (snap->GetSnapType() == SnapType::StartPoint || snap->GetSnapType() == SnapType::EndPoint)
            {
                const auto& nodeRefs = snap->GetSnappedGraphicsNodeReferences();
                FOR_EACH(nodeRef, nodeRefs)
                {
 
                    const IElement* pElement = pDoc->GetElement(nodeRef->GetElementId());
                    Int64 type = typeid(*pElement).hash_code();
                    auto it = std::find(m_types.begin(), m_types.end(), type);
                    if (it != m_types.end())
                        return true;
                }
            }

            const ICombinedSnap* pCombineSnap = dynamic_cast<const ICombinedSnap*>(snap);
            if (pCombineSnap)
            {
                std::vector<const ISnap*> subSnaps = pCombineSnap->GetSubSnaps();
                FOR_EACH(subSnap, subSnaps)
                {
                    if (!subSnap)
                        continue;

                    if (IsEndPointInTypes(subSnap))
                        return true;
                }
            }

            return false;
        }

        const std::vector<Int64> m_types;
    };
}

GbmpSnapEndPointPriorityTypesPreprocessor::GbmpSnapEndPointPriorityTypesPreprocessor(const std::vector<Int64>& types) :
    m_types(types)
{
}

void GbmpSnapEndPointPriorityTypesPreprocessor::Process(std::vector<OwnerPtr<ISnap>>& values)
{
    if (values.size() > 1)
    {
        std::stable_sort(values.begin(), values.end(), Comparer(m_types));
    }
    return;
}

